package org.example.controller;

import org.example.TimeCurrency;
import org.example.contracts.Structs;
import org.example.config.*;
import org.fisco.bcos.sdk.client.Client;
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * @program: asset-app
 * @description:
 * @author: car
 * @create: 2024-04-02 11:46
 **/

@RestController
public class FiscoController {
    @Resource
    private FiscoBcos fiscoBcos;

    @RequestMapping("block")
    public String Number() throws ContractException {
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();

        Structs structs = Structs.load("0xb4f80c4941f150a2d251e598e991bc344b954a4f",
                client, cryptoKeyPair);
        BigInteger id = new BigInteger("1");
        String name = structs.read(id);
        return name;
    }

    @GetMapping("/getOneTimeCurrency")
    public TimeCurrency.Struct0 getOneTimeCurrency(@RequestParam(value = "userId") String userId) throws ContractException {
        TimeCurrency timeCurrency = getTimeCurrency();
        TimeCurrency.Struct0 user = timeCurrency.getUser(userId);
        return user;
    }

    @PostMapping("/setOneInit")
    public TransactionReceipt setOneInit(String userId, String userName) {
        TimeCurrency timeCurrency = getTimeCurrency();
        TransactionReceipt transactionReceipt = timeCurrency.setUser(userId, userName);
        return transactionReceipt;
    }

    @PostMapping("/transition")
    public TransactionReceipt transition(String sendId, String getId, Integer money) {
        TimeCurrency timeCurrency = getTimeCurrency();
        return timeCurrency.transition(sendId, getId, BigInteger.valueOf(money));
    }

    public TimeCurrency getTimeCurrency() {
        Client client = fiscoBcos.getBcosSDK().getClient(Integer.valueOf(1));
        CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().getCryptoKeyPair();
        TimeCurrency timeCurrency = TimeCurrency.load("0x6022d39c791079164b37f71b05e676fd8aa1f337",
                client,
                cryptoKeyPair);
        return timeCurrency;
    }


}
